package com.mjw.tender.controller;

import com.mjw.tender.annotation.AvoidRepeatableCommit;
import com.mjw.tender.api.BlogUserService;
import com.mjw.tender.api.CommentService;
import com.mjw.tender.domain.Comment;
import com.mjw.tender.domain.CommentB;
import com.mjw.tender.dto.CommentDto;
import com.mjw.tender.dto.ResponseResult;
import com.mjw.tender.dto.SecondCommonDto;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class CommentController {

    @Reference(version = "1.0.0")
    CommentService commentService;

    @Reference(version = "1.0.0")
    BlogUserService blogUserService;


    @AvoidRepeatableCommit(timeout = 3000)
    @PostMapping("/firstComment")
    public ResponseResult addCommment(@RequestBody Comment comment){
        int i = commentService.addComment(comment);
        return new ResponseResult(ResponseResult.CodeStatus.OK,"添加成功");
    }

    @AvoidRepeatableCommit(timeout = 3000)
    @PostMapping("/secondComment")
    public ResponseResult addCommentB(@RequestBody CommentB commentB){
        int i = commentService.addCommentB(commentB);
        return new ResponseResult(ResponseResult.CodeStatus.OK,"回复成功");
    }



    @GetMapping("/comment")
    public ResponseResult<List<CommentDto>> initComment(@RequestParam("articleId")Long articleId,
                                                        @RequestParam("userId")Long userId){
        List<CommentDto> commentDtoList =  commentService.getCommentDto(articleId,userId);//查询一级评论列表
        List<SecondCommonDto> secondCommonDtoList = commentService.SecondCommonDto(articleId);//查询二级评论列表
        //遍历二级评论表
        Map<Long,List<SecondCommonDto>> map = new HashMap();//用一个map把二级列表按一级评论id分组
        for(int i=0;i<secondCommonDtoList.size();i++){
            Long commentId = secondCommonDtoList.get(i).getCommentId();//一级评论ID
            if(!map.containsKey(commentId)){
                List<SecondCommonDto> reply = new ArrayList();
                reply.add(secondCommonDtoList.get(i));
                map.put(commentId,reply);
            }else {
                map.get(commentId).add(secondCommonDtoList.get(i));
            }
        }

        for(int i= 0;i<commentDtoList.size();i++){
            Long commentId = commentDtoList.get(i).getId(); //一级评论ID
            commentDtoList.get(i).setReply(map.get(commentId));
        }

        return new ResponseResult<List<CommentDto>>(ResponseResult.CodeStatus.OK,commentDtoList);
    }

    @GetMapping("/comment/recent")
    public ResponseResult<List<CommentDto>> getRecentComment(@RequestParam("num")Integer num){
        List<CommentDto> list = commentService.getRecentComment(num);
        return new ResponseResult(ResponseResult.CodeStatus.OK,list);
    }

}
